/*
 *   Copyright (C) 2009  Jens Kieselbach
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You may obtain a copy of the License at http://www.gnu.org/licenses/gpl.html
 */
package de.kieselbach.grootional.util

import de.kieselbach.grootional.*
import de.kieselbach.grootional.value.*
import de.kieselbach.swing.*
import java.sql.*

class DatabaseUtils {

    static def getConnection(MainFrame frame, ConnectionValue value) {

        Connection con = null

        PaneBuilder.runWithMonitor(frame.frame, frame.resourceBundle.getString('util.gettingconnection'), frame.resourceBundle.getString('util.getconnection'), frame.resourceBundle.getString('util.getfailed')) {

            con = getConnection(value)
        }

        con
    }

    static def getConnection(ConnectionValue value) {

        Class.forName(value.driver)

        Properties conprops = new Properties()
        conprops.put('user', value.user)
        conprops.put('password', value.password)

        if(value.properties != null) {
            value.properties.each() {prop ->
                conprops.put(prop.key, prop.value)
            }
        }

        def con = DriverManager.getConnection(value.url, conprops)
        con.setAutoCommit(false)
        con
    }

    static def String formatJdbcType(int type) {

        switch (type) {
            case Types.ARRAY:
                return 'ARRAY'
            case Types.BIGINT:
                return 'BIGINT'
            case Types.BINARY:
                return 'BINARY'
            case Types.BIT:
                return 'BIT'
            case Types.BLOB:
                return 'BLOB'
            case Types.BOOLEAN:
                return 'BOOLEAN'
            case Types.CHAR:
                return 'CHAR'
            case Types.CLOB:
                return 'CLOB'
            case Types.DATALINK:
                return 'DATALINK'
            case Types.DATE:
                return 'DATE'
            case Types.DECIMAL:
                return 'DECIMAL'
            case Types.DISTINCT:
                return 'DISTINCT'
            case Types.DOUBLE:
                return 'DOUBLE'
            case Types.FLOAT:
                return 'FLOAT'
            case Types.INTEGER:
                return 'INTEGER'
            case Types.JAVA_OBJECT:
                return 'JAVA_OBJECT'
            case Types.LONGVARBINARY:
                return 'LONGVARBINARY'
            case Types.LONGVARCHAR:
                return 'LONGVARCHAR'
            case Types.NULL:
                return 'NULL'
            case Types.NUMERIC:
                return 'NUMERIC'
            case Types.OTHER:
                return 'OTHER'
            case Types.REAL:
                return 'REAL'
            case Types.REF:
                return 'REF'
            case Types.SMALLINT:
                return 'SMALLINT'
            case Types.STRUCT:
                return 'STRUCT'
            case Types.TIME:
                return 'TIME'
            case Types.TIMESTAMP:
                return 'TIMESTAMP'
            case Types.TINYINT:
                return 'TINYINT'
            case Types.VARBINARY:
                return 'VARBINARY'
            case Types.VARCHAR:
                return 'VARCHAR'
            default:
                return String.valueOf(type)
        }
    }
}
